﻿$(function () {
    $("#outPutWord").live("click", outPutWordClick);
    $("#btnEditBasicInfo").toggle(basicEdit, basicSaveClick);
    $("#btnSubmit").click(SumbitRegistrationForm);
    $("#btnAddEducations").click(btnAddEducationsClick);//学历
    $(".btnEditEducations").live("click", educationEdit);
    $(".btnSaveEducations").live("click", educationSave);

    $("#btnAddJobs").click(btnAddJobsClick);//工作经历
    $(".btnEditJobs").live("click", jobEdit);
    $(".btnSaveJobs").live("click", jobSave);


    $("#btnAddFamilays").click(btnAddFamilaysClick);//家庭成员
    $(".btnEditFamilays").live("click", familayEdit);
    $(".btnSaveFamilays").live("click", familaySave);

    $("#btnEditSelfInfo").toggle(selfInfoEdit, selfInfoSave);//自我评定
    $(".Wdate").live("focus", txtdateConfig);//日期控件配置

    $("#btnSaveProject").click(btnSaveProjectClick);//出国项目保存

    function outPutWordClick() {
        var content = $("#InfoContainer").html();
        ExpHtmlToWord(content)
    }
    $(".closeButton").click(function () {
        $("#divProject").hide();
    });

    $("#textProjectSelected").click(function () {
        $(this).blur();
        $("#divProject").show();
    });

    $("#textProjectSelected").val($("#hiddenSelectedProject").val());//已选项目

    textBoxdisEnabled();
    seldisEnabled(false);
    chkboxdisEnabled(false);

    //文本控件只读控制
    function textBoxdisEnabled() {

        //基本信息
        bindInput("#tableBasic input[type='text']"); //默认文本框不可修改
        bindInput("#Certificate");

        //教育
        bindInput("#tbEduncation input[type='text']"); //默认文本框不可修改

        //工作
        bindInput("#tbJob input[type='text']"); //默认文本框不可修改

        //家庭
        bindInput("#tbFamilay input[type='text']"); //默认文本框不可修改

        //自我评定
        bindInput("#tbSelfInfo input[type='text']");
    }

    //下拉框
    function seldisEnabled(flag) {
        if (!flag) {
            $("#tableBasic select").attr("disabled", "disabled");
        } else {
            $("#tableBasic select").removeAttr("disabled");
        }
    }

    //勾选框
    function chkboxdisEnabled(flag) {
        if (!flag) {
            $("#tableBasic input[type='radio']").attr("disabled", "disabled");
        } else {
            $("#tableBasic input[type='radio']").removeAttr("disabled");
        }
    }


    //解绑进入焦点事件
    function unbindInput(dom, isJqObj) {
        if (isJqObj == undefined) {
            $(dom).die("focus");
        } else if (isJqObj) {
            dom.die("focus");
        }

    }

    //绑定
    function bindInput(dom) {
        $(dom).live("focus", function () {
            this.blur();
        });
    }

    //日历控件初始化
    function txtdateConfig() {
        var endValue = new Date();
        WdatePicker({
            minDate: '1900-01-01', maxDate: endValue,
            onpicked: function () {
                var $this = $(this);
                //前一个控件的id
                if ($this.attr("data-preId") != undefined) {
                    var preVal = $("#" + $this.attr("data-preId")).val();
                    if (new Date($this.val()) < new Date(preVal)) {
                        alert("截止日期不得小于开始日期");
                        $this.val("").click();
                    }
                }

                if ($this.attr("data-nextId") != undefined) {
                    var nextVal = $("#" + $this.attr("data-nextId")).val();
                    if (new Date($this.val()) > new Date(nextVal)) {
                        alert("开始日期不得大于截止日期");
                        $this.val("").click();
                    }
                }

            }
        });
    }

    function commonBind($this, tbId) {
        var $tr = $this.parents("tr").first();
        var inputs = $tr.find("input[type='text']");
        inputs.find(".date_txt").removeClass("Wdate");
        inputs.removeClass("inputEdit");
        bindInput("#" + tbId + " input[type='text']"); //默认文本框不可修改
    }

    function commonUnBind($this, tbId) {
        var $tr = $this.parents("tr").first();
        var inputs = $tr.find("input[type='text']");
        inputs.filter(".date_txt").addClass("Wdate");
        inputs.addClass("inputEdit");
        unbindInput($("#" + tbId + " input[type='text']"), true); //默认文本框不可修改
        inputs.first().click();
    };

    //家庭成员
    function btnAddFamilaysClick() {
        $tbody = $("#tbFamilay tbody");
        var $tr = $("<tr>");
        $td1 = $("<td>", { html: "<input type='text' name='Name' />" });
        $td2 = $("<td>", { html: "<input type='text' name='Age' />" });
        $td3 = $("<td>", { html: "<input type='text' name='Relationship' />" });
        $td4 = $("<td>", { html: "<input type='text' name='Compay' />" });
        $td5 = $("<td>", { html: "<input type='text' name='JobRoler' />" });
        $td6 = $("<td>", { html: "<button type='button' class='btn btn-link btn-xs btnSaveFamilays'><span class='glyphicon glyphicon-floppy-disk'></span>&nbsp;保存</button>" });
        $tr.append($td1).append($td2).append($td3).append($td4).append($td5).append($td6);
        $tbody.append($tr);
        commonUnBind($tr, "tbFamilay");
    }

    //家庭成员编辑
    function familayEdit() {
        var $this = $(this);
        $this.removeClass("btnEditFamilays").addClass("btnSaveFamilays").html("<span class='glyphicon glyphicon-floppy-disk'></span>&nbsp;保存");
        commonUnBind($this, "tbFamilay");
    }

    //家庭成员保存
    function familaySave() {
        var $this = $(this);
        var RegistrationFamilayId = $this.attr("data-id");
        var $tr = $this.parents("tr").first();
        var Name = $tr.find("td").eq(0).find("input[name='Name']").eq(0).val();
        var Age = $tr.find("td").eq(1).find("input[name='Age']").eq(0).val();
        var Relationship = $tr.find("td").eq(2).find("input[name='Relationship']").eq(0).val();
        var Compay = $tr.find("td").eq(3).find("input[name='Compay']").eq(0).val();
        var JobRoler = $tr.find("td").eq(4).find("input[name='JobRoler']").eq(0).val();
        //alert(Name + '-' + Age + '-' + Relationship + '-' + Compay + '-' + JobRoler + '-' + RegistrationFamilayId);
        var data = {
            Name: Name, Age: Age, Relationship: Relationship, Compay: Compay,
            JobRoler: JobRoler, RegistrationFamilayId: RegistrationFamilayId
        };
        var url = "../Trainee/SaveFamilay";
        var newStr = "操作中";
        $.ajax({
            url: url,
            data: data,
            dataType: "json",
            beforeSend: function () {
                $this.text(newStr);
                LoadlockDiv();
            },
            type: "post",
            success: function (res) {
                if (res.Result.IsSuccess) {
                    alert("保存成功");
                    commonBind($this, "tbFamilay");
                }
                else {
                    alert(res.Result.ErrMessage);
                }
            },
            async: true,
            complete: function () {
                removeLockDiv();
                $this.removeClass("btnSaveFamilays").addClass("btnEditFamilays").html("编辑");
            }
        });
    };

    //工作经历
    function btnAddJobsClick() {
        $tbody = $("#tbJob tbody");
        var $tr = $("<tr>");
        $td1 = $("<td>", { html: "<input type='text' name='StartDate' style='width:88px' class='date_txt input_font Wdate' />-<input  style='width:88px'  type='text' name='EndDate'  class='date_txt input_font Wdate' /> " });
        $td2 = $("<td>", { html: "<input type='text' name='Company' />" });
        $td3 = $("<td>", { html: "<input type='text' name='WorkRoler' />" });
        $td4 = $("<td>", { html: "<input type='text' name='WordDes' />" });
        $td5 = $("<td>", { html: "<input type='text' name='Salary' />" });
        $td6 = $("<td>", { html: "<button type='button' class='btn btn-link btn-xs btnSaveJobs'><span class='glyphicon glyphicon-floppy-disk'></span>&nbsp;保存</button>" });
        $tr.append($td1).append($td2).append($td3).append($td4).append($td5).append($td6);
        commonUnBind($tr, "tbJob");
        $tbody.append($tr);
    }

    //工作编辑
    function jobEdit() {
        var $this = $(this);
        $this.removeClass("btnEditJobs").addClass("btnSaveJobs").html("<span class='glyphicon glyphicon-floppy-disk'></span>&nbsp;保存");

        commonUnBind($this, "tbJob");
    }

    //工作保存
    function jobSave() {
        var $this = $(this);
        var RegistrationJobId = $this.attr("data-id");
        var $tr = $this.parents("tr").first();
        var StartDate = $tr.find("td").eq(0).find("input[name='StartDate']").eq(0).val();
        var EndDate = $tr.find("td").eq(0).find("input[name='EndDate']").eq(0).val();
        var Company = $tr.find("td").eq(1).find("input[name='Company']").eq(0).val();
        var WorkRoler = $tr.find("td").eq(2).find("input[name='WorkRoler']").eq(0).val();
        var WordDes = $tr.find("td").eq(3).find("input[name='WordDes']").eq(0).val();
        var Salary = $tr.find("td").eq(4).find("input[name='Salary']").eq(0).val();
        // alert(StartDate + '-' + EndDate + '-' + Company + '-' + WorkRoler + '-' + WordDes + '-' + Salary + '-' + RegistrationJobId);
        var data = {
            StartDate: StartDate, EndDate: EndDate, Company: Company,
            WorkRoler: WorkRoler, WordDes: WordDes, Salary: Salary, RegistrationJobId: RegistrationJobId
        };
        var url = "../Trainee/SaveJob";
        var newStr = "操作中";
        $.ajax({
            url: url,
            data: data,
            dataType: "json",
            beforeSend: function () {
                LoadlockDiv();
                $this.text(newStr);
            },
            type: "post",
            success: function (res) {
                if (res.Result.IsSuccess) {
                    alert("保存成功");
                    commonBind($this, "tbJob");
                }
                else {
                    alert(res.Result.ErrMessage);
                }
            },
            async: true,
            complete: function () {
                removeLockDiv();
                $this.removeClass("btnSaveJobs").addClass("btnEditJobs").text("编辑");
            }
        });
    };

    //教育信息
    function btnAddEducationsClick() {
        $tbody = $("#tbEduncation tbody");
        var $tr = $("<tr>");
        $td1 = $("<td>", { html: "<input type='text' name='StartDate' style='width:100px' class='date_txt input_font Wdate' />-<input type='text' name='EndDate' style='width:100px' class='date_txt input_font Wdate'/>" });
        $td2 = $("<td>", { html: "<input type='text' name='School' width='100%'/>" });
        $td3 = $("<td>", { html: "<input type='text' name='Major' width='100%'/>" });
        $td4 = $("<td>", { html: "<button type='button' class='btn btn-link btn-xs btnSaveEducations'><span class='glyphicon glyphicon-floppy-disk'></span>&nbsp;保存</button>" });
        $tr.append($td1).append($td2).append($td3).append($td4);
        $tbody.append($tr);
        commonUnBind($tr, "tbEduncation");
    }

    //学历编辑
    function educationEdit() {
        var $this = $(this);
        $this.removeClass("btnEditEducations").addClass("btnSaveEducations").html("<span class='glyphicon glyphicon-floppy-disk'></span>&nbsp;保存");

        commonUnBind($this, "tbEduncation");
    }

    //学历保存
    function educationSave() {
        var $this = $(this);
        var RegistrationEducationId = $this.attr("data-id");
        var $tr = $this.parents("tr").first();
        var StartDate = $tr.find("td").eq(0).find("input[name='StartDate']").eq(0).val();
        var EndDate = $tr.find("td").eq(0).find("input[name='EndDate']").eq(0).val();
        var School = $tr.find("td").eq(1).find("input[name='School']").eq(0).val();
        var Major = $tr.find("td").eq(2).find("input[name='Major']").eq(0).val();
        //alert(StartDate + '-' + EndDate + '-' + School + '-' + Major + '-' + RegistrationEducationId);
        var data = {
            StartDate: StartDate, EndDate: EndDate, School: School,
            Major: Major, RegistrationEducationId: RegistrationEducationId
        };
        var url = "../Trainee/SaveEducation";
        var newStr = "操作中";
        $.ajax({
            url: url,
            data: data,
            dataType: "json",
            beforeSend: function () {
                LoadlockDiv();
                $this.text(newStr);
            },
            type: "post",
            success: function (res) {
                if (res.Result.IsSuccess) {
                    alert("保存成功");

                    commonBind($this, "tbEduncation");

                }
                else {
                    alert(res.Result.ErrMessage);
                }
            },
            async: true,
            complete: function () {
                removeLockDiv();
                $this.removeClass("btnSaveEducations").addClass("btnEditEducations").text("编辑");
            }
        });
    };

    function getWorkTypeStr() {
        var workTypeStr = "";
        var checkedWorkType = $(".chkWorkType:checked");
        $.each(checkedWorkType, function (key, value) {
            var $this = $(value);
            workTypeStr += $this.val() + "$";
        });
        return workTypeStr;
    }
    //出国项目保存
    function btnSaveProjectClick() {
        var workTypeStr = getWorkTypeStr();
        basicSaveNewJobs({ newJobs: workTypeStr });
    }

    function basicSaveNewJobs(data) {
        var url = "../Trainee/SaveProjectInfo";
        $.ajax({
            url: url,
            data: data,
            dataType: "json",
            beforeSend: function () {
                LoadlockDiv();
            },
            type: "post",
            success: function (res) {
                if (res.Result.IsSuccess) {
                    alert("保存成功", function () {
                        $("#divProject").hide();
                        location.reload();
                    });
                }
                else {
                    alert(res.Result.ErrMessage);
                }
            },
            async: true,
            complete: function () {
                removeLockDiv();
            }
        });
    };

    //自我评定编辑
    function selfInfoEdit() {
        var $this = $(this);
        $this.html("<span class='glyphicon glyphicon-floppy-disk'></span>&nbsp;保存");

        unbindInput("#tbSelfInfo input[type='text']");
        var inputs = $("#tbSelfInfo input[type='text']");
        inputs.first().focus();
        inputs.addClass("inputEdit");

    }

    //自我评定保存
    function selfInfoSave() {
        var $selfInfoForm = $("#selfInfoForm");//自我评定
        data = $selfInfoForm.serialize();
        basicSaveIntroduce(data,$(this))
    }

    function basicSaveIntroduce(data,$this) {
        var url = "../Trainee/SaveIntroduce";
        $.ajax({
            url: url,
            data: data,
            dataType: "json",
            beforeSend: function () {
                LoadlockDiv();
            },
            type: "post",
            success: function (res) {
                if (res.Result.IsSuccess) {
                    alert("保存成功", function () {
                        var inputs = $("#tbSelfInfo input[type='text']");
                        bindInput("#tbSelfInfo input[type='text']");
                        inputs.removeClass("inputEdit");
                    });
                }
                else {
                    alert(res.Result.ErrMessage);
                }
            },
            async: true,
            complete: function () {
                removeLockDiv();
                $this.html("<span class='glyphicon glyphicon glyphicon-pencil'></span>&nbsp;编辑");
            }
        });
    };


    //基本信息编辑
    function basicEdit() {
        var $this = $(this);
        $this.html("<span class='glyphicon glyphicon-floppy-disk'></span>&nbsp;保存");
        $("#divResumeImageBtn").show();
        unbindInput("#tableBasic input[type='text']");
        unbindInput("#Certificate");
        var inputs = $("#tableBasic input[type='text']");
        inputs.first().focus();
        inputs.addClass("inputEdit");
        $("#Certificate").addClass("inputEdit");
        seldisEnabled(true);
        chkboxdisEnabled(true);
        $("#Birthday").addClass("Wdate");//生日日历控件
    }

    //基本信息保存
    function basicSaveClick() {
        var workTypeStr = getWorkTypeStr();//出国项目
        var $basicInfoform = $("#basicInfoForm");//基本信息
        var $selfInfoForm = $("#selfInfoForm");//自我评定
        var data = $basicInfoform.serialize();
        data += "&" + $selfInfoForm.serialize();
        data += "&NewJobs=" + workTypeStr;
        basicSave($(this), data, function () {
            var inputs = $("#tableBasic input[type='text']");
            bindInput("#tableBasic input[type='text']");
            inputs.removeClass("inputEdit");
            $("#Certificate").removeClass("inputEdit");
            bindInput("#Certificate");
            seldisEnabled(false);
            chkboxdisEnabled(false);
            $("#Birthday").removeClass("Wdate");//生日日历控件
        });
    }

    //由于自我评定涉及字段和基本信息在同一张表中 所以共用一张表
    function basicSave($this, data, fun, isChange) {
        if (isChange == undefined) {
            isChange = true;
        }
        var newStr = "操作中";
        var url = "../Trainee/SaveBasicInfo";
        $.ajax({
            url: url,
            data: data,
            dataType: "json",
            beforeSend: function () {
                LoadlockDiv();
                if (isChange) {
                    $this.text(newStr);
                }
            },
            type: "post",
            success: function (res) {
                if (res.Result.IsSuccess) {
                    alert("保存成功");
                    fun();
                }
                else {
                    alert(res.Result.ErrMessage);
                }
            },
            async: true,
            complete: function () {
                removeLockDiv();
                if (isChange) {
                    $this.html("<span class='glyphicon glyphicon glyphicon-pencil'></span>&nbsp;编辑");
                }
            }
        });
    };

    //实习项目选择检测
    function checkWorkType() {
        var res = true;
        var selectedWorkType = $("#hiddenSelectedProject").val();
        if (!$.trim(selectedWorkType)) {
            alert("请选择实习项目");
            $("#textProjectSelected").click();
            res = false;
        }
        return res;
    }

    //教育检测
    function checkEdcuation() {
        var res = true;
        var $trs = $("#tbEduncation tbody tr");
        if ($trs.length == 0) {
            alert("填写教育经历");
            res = false;
        }
        return res;
    }

    //基础信息
    function checkBasicInfo() {
        return true;
    }

    //工作经历
    function checkJobHis() {
        return true;
    }

    //家庭
    function checkFamily() {
        return true;
    }

    //自我评测
    function checkSelfInfo() {
        return true;
    }

    function SumbitFormCheck() {
        if (!checkWorkType()) {
            return false;
        }

        if (!checkBasicInfo()) {
            return false;
        }

        if (!checkEdcuation()) {
            return false;
        }

        if (!checkJobHis()) {
            return false;
        }

        if (!checkFamily()) {
            return false;
        }

        if (!checkSelfInfo()) {
            return false;
        }

        return true;
    }

    function SumbitRegistrationForm() {
        if (!SumbitFormCheck()) {
            return;
        }
        var $this = $(this);
        var url = "../Trainee/SumbitRegistrationForm";
        $.ajax({
            url: url,
            dataType: "json",
            beforeSend: function () {
                LoadlockDiv();
                $this.html("操作中...");
            },
            type: "post",
            success: function (res) {
                if (res.Result.IsSuccess) {
                    alert("提交成功,等待管理员处理", function () { location.href = "../Trainee" });
                }
                else {
                    alert(res.Result.ErrMessage);
                }
            },
            async: true,
            complete: function () {
                $this.html("提交");
                removeLockDiv();
            }
        });
    };

});